//
//  HMProductListCollectionViewCell.m
//  HMMarket
//
//  Created by maimaigo3 on 2017/5/16.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMProductListCollectionViewCell.h"
#import "HMLastPro.h"

@interface HMProductListCollectionViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *titleImageVI;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UIButton *cartBtn;
@property (weak, nonatomic) IBOutlet UIImageView *sepacialPriceImageVI;

@end

@implementation HMProductListCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setProDuctModel:(HMLastPro *)proDuctModel {
    MyLog(@"proDuctModel.is_hot = %@", proDuctModel.is_hot);
    _proDuctModel = proDuctModel;
    [[SDImageCache sharedImageCache] clearMemory];
    if ([_proDuctModel.is_hot isEqualToString:@"特价菜品"]) {
        _sepacialPriceImageVI.hidden = NO;
    } else {
        _sepacialPriceImageVI.hidden = YES;
    }
    NSString *string = [NSString stringWithFormat:@"%@%@",kImageUrl,_proDuctModel.logo];
    [_titleImageVI sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"ProductList_PlaceHolder"]];
    _nameLabel.text = proDuctModel.item_name;
    _priceLabel.text = [NSString stringWithFormat:@"¥%@/%@", proDuctModel.item_price, proDuctModel.weight] ;
    
    [_cartBtn addTarget:self action:@selector(addProduct:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)addProduct:(UIButton *)sender{
    sender.userInteractionEnabled = NO;
    UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    CGRect rect=[_titleImageVI convertRect: _titleImageVI.bounds toView:window];
    
    if ([_delegate respondsToSelector:@selector(sendIndex:frame:imageVI:)]) {
        [_delegate sendIndex:_index frame:rect imageVI:_titleImageVI.image];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        sender.userInteractionEnabled = YES;
    });
}

@end
